package net.chat.utils;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
/**
 * 
 * @author Amir
 * using SHA algorithm and guarantee that if hacker has encrypted password can not
 * find the real password
 */
public class Encryptor
{
  public static String encrypt(String originalText) throws Exception
  {
    MessageDigest messageDigest = null;
    byte digestResult[];
    try
    {
      messageDigest = MessageDigest.getInstance("SHA");
      // UTF-8 byte representation for string
      messageDigest.update(originalText.getBytes("UTF-8"));
    }
    catch(Exception e)
    {
      throw new Exception(e.getMessage());
    }
    //byte array that has the digested result
    digestResult = messageDigest.digest();
    //convert bytes to string in format of base64 that is a readable character format
    return (new BASE64Encoder()).encode(digestResult);
  }
}
//How to use it
/*
 * encriptedPass=Encryptor.encrypt(originalPassword);
 * then you can compare it with password on the server.
 * Always password on the server(array list) is kept encrypted
 * It can not be converted to original password in anyway
 */